local japi = require 'jass.japi'
local jass = require 'jass.common'

--[[
    japi.SimpleFrameFindByName("SimpleInfoPanelUnitDetail",0) 单位面板
    japi.SimpleFrameFindByName("SimpleInfoPanelIconHero",6), 英雄面板
]]

ac.frame:load('simpleClass',[[
    //Simple层级
    Frame "SIMPLEFRAME" "simpleClass"{
        Frame "BACKDROP" "simpleBackdrop"
        {}
    }
]])

local simple = {}
setmetatable(simple,simple)

--- 将一个simpleframe转换为对象格式
function simple.to(handle)
    return setmetatable({
        handle = handle,
        children = { },
        simple_flag = true,
        hide_has_event = true,
        w = 1920,
        h = 1080,
    },{__index = class.ui_base})
end

--- 存放所有面板
simple.allsimples = {}

--- 输出
function simple:__tostring()
    return string.format('[简易面板]->%d',self.handle or 0)
end 

local mt = extends(class.panel){}

--- 类型
mt.type = 'simple'

--- 识别名
mt._name = '简易面板'

--- 初始化
function mt:init()
    if self.handle then return end 
    class.ui_base.init(self)
    if not self.parent:is_simple() then 
        log.error('父级必须是simpleFrame类型')
        class.ui_base.remove(self)
        return 
    end
    local index = ac.frame:compose('simpleClass')
    local _handle = japi.CreateSimpleFrame(("simpleClass(%d)"):format(index),self.parent.handle, index)
    if _handle == nil or _handle == 0 then 
        log.error('创建simple失败')
        class.ui_base.remove(self)
        return 
    end 
    self._handle = _handle
    japi.FrameSetAbsolutePoint(self._handle,0,0)
    japi.FrameShow(self._handle,true)
    local handle = japi.FrameFindByName(("simpleBackdrop(%d)"):format(index),index)
    if handle == nil or handle == 0 then 
        log.error('寻找simple依托面板失败')
        japi.DestroySimpleFrame(self._handle)
        class.ui_base.remove(self)
        return 
    end 
    self.handle = handle
    simple.allsimples[handle] = self
    class.ui_base.allframes[handle] = self
    self:set_normal_image(self.normal_image)
    self:set_alpha(self.alpha)
    self:set_width(self.w)
    self:set_height(self.h)
    self:set_position(self.x,self.y)
    self:set_view_port(self.view_port)
    if self.show_flag then
        self.show_flag = false
        self:show()
    else 
        self.show_flag = true
        self:hide()
    end
    return self
end 

--- 销毁
function mt:remove()
    if self.removed then return end 
    simple.allsimples[self.handle] = nil
    class.ui_base.remove(self)
    japi.DestroySimpleFrame(self._handle)
end 

---设置位置
function mt:set_position(x,y)
    if self.removed then return end
    self.x = x
    self.y = y
    japi.FrameSetAbsolutePoint(self.handle,0,x / 1920 * 0.8,(1080 - y) / 1080 * 0.6)
end

class.simple = mt

--- 镜头模型的父级 这个可以用来绑定血条 
class.ui_base.portrait = simple.to(japi.FrameGetParent(japi.FrameGetPortrait()))

--- 创建一个单位面板为依托的simple层级
class.ui_base.unitdetail = class.simple:builder{parent = simple.to(japi.SimpleFrameFindByName("SimpleInfoPanelUnitDetail", 0)),normal_image = '',show_flag = false,simple_flag = true,default = true}

--- 创建一个英雄面板为依托的simple层级
class.ui_base.iconhero = class.simple:builder{parent = simple.to(japi.SimpleFrameFindByName("SimpleInfoPanelIconHero", 6)),normal_image = '',show_flag = false,simple_flag = true,default = true}

class.ui_base.itemdetail = class.simple:builder{parent = simple.to(japi.SimpleFrameFindByName("SimpleInfoPanelItemDetail", 3)),normal_image = '',show_flag = false,simple_flag = true,default = true}

-- model2黑底渲染区域
class.ui_base._model_backdrop = class.model:builder{
    parent = class.ui_base.portrait,
    model = [[ui\_model_backdrop.mdx]],
    y = -240,
    scales = {20,8,1,},
    alpha = 0.02,
}

--- 预处理
ac.message:on '选择单位' (function(_, unit)
    if not class.ui_base.iconhero:is_show() then 
        if jass.IsUnitType(unit,0) then 
            class.ui_base.iconhero:show()
        end 
    end 
    if not class.ui_base.unitdetail:is_show() then 
        class.ui_base.unitdetail:show()
    end 
end)



return mt
